
/* 

NOTE: Table 2 has four columns that present analyses on economic and humanitarian aid in two periods, 
pre-2003 and post-2003. To replicate the analyses for either period, go to lines 19 and 20 and 
run the code that keeps observations for the correct period in the dataset. Do not forget to comment out
the code line that keeps the other period's observations. For example, if you want to replicate pre-2003 analyses,
then you must comment out line 20.

*/


* LOAD DATA
use "C:\Users\suuser\Dropbox\Turkish Foreign Aid\empirics\2017-11 (replication data)\main data 2017-11-24.dta", clear


preserve

* select TIME PERIOD: pre2003 or post2003
keep if pre2003 ==1
* keep if post2003 ==1


* drop observations where (1) the recipent is not a low/middle income country, or (2) the donor did not give any aid to any country in that year
keep if notinDAC ==0 & noaid_cyears ==0

* Define MACROS to make the code tidier

local depvar		"dvhigh dvlag"

local donor 		"c.donor_gdppc c.donor_gdpgrowth c.donor_victims"

local culturalties	"c.donor_lang_pct c.donor_relig_pct"

local needs			"c.recip_infantmort c.recip_gdppc"

local governance	"c.recip_polity2 c.recip_polterror"

	
* specify interaction variable: "OECD_DAC"
gen period_dummy =.
replace period_dummy = oecd_dac

local combined		"i.period_dummy##(`culturalties' l(c.donor_exports c.un_vote_sim) l(`needs') c.recip_victims l(`governance') l(c.ln_distance c.recip_population) )"


****************
* Economic Aid *
****************

* Prepare dependent variable
gen dvlow = ln_econ_aid_dv 
replace dvlow =. if ln_econ_aid_dv <=0
gen dvhigh = ln_econ_aid_dv 
replace dvhigh =0 if dvhigh <0
gen dvlag = l.dvhigh
		

* Tobit regression (estimates saved, but not shown)
quietly: tobit `depvar'	`combined' `donor'		 	///
	, ll(0) vce(cluster ccode2)
	est store ec_dac_tobit	
	
* calculate MARG FX for every variable
margins, dydx(*) predict(ystar(0,.)) at(period_dummy=(0 1))	post
	est store ec_dac_margfx

* run regression again
quietly: tobit `depvar'	`combined' `donor'			 	///
	, ll(0) vce(cluster ccode2)
	sum year if e(sample)
	
* calculate: P-VALUE for marg'l effect of EVERY VARIABLE
margins, dydx(*) predict(ystar(0,.)) at(period_dummy=(0 1)) contrast(atcontrast(r))	post
	est store ec_dac_pvalue
	
drop dv*
	

********************
* Humanitarian Aid *
********************

* Prepare dependent variable
tsset dyadnum year
gen dvlow = ln_human_aid_dv 
replace dvlow =. if ln_human_aid_dv <=0
gen dvhigh = ln_human_aid_dv 
replace dvhigh =0 if dvhigh <0
gen dvlag = l.dvhigh


* Tobit regression (estimates saved, but not shown)
quietly: tobit `depvar' `combined' `donor'			 	///
	if disaster_D ==1, ll(0) vce(cluster ccode2)
	est store hum_dac_tobit

* calculate MARG FX for every variable
margins, dydx(*) predict(ystar(0,.)) at(period_dummy=(0 1))	post
	est store hum_dac_margfx

* run regression again
quietly: tobit `depvar' `combined' `donor'	 			 	///
	if disaster_D ==1, ll(0) vce(cluster ccode2)
	
* calculate: P-VALUE for marg'l effect of EVERY VARIABLE
margins, dydx(*) predict(ystar(0,.)) at(period_dummy=(0 1)) contrast(atcontrast(r))	post
	est store hum_dac_pvalue

drop dv*


**********
* TABLES *
**********


* Produce TABLE 2 in article (Marginal Effects)
esttab *ec*dac*margfx* *hum*dac*margfx*, b(3) se(3) 			///
	stats(N ll) noomit nobase noconstant compress mtitles wide

	
* List of P-VALUES for TABLE 2
estimates replay *ec*pvalue* *hum*pvalue*	

